import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/cart.dart';
import '../provide/person.dart';
import '../provide/details_info.dart';
import '../provide/currentIndex.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DetailsBottom extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    var goodsInfo = Provide.value<DetailsInfoProvide>(context).goodsInfo;
    var goodsId= goodsInfo['ID'];
    var goodsName =goodsInfo['NAME'];
    var count =1;
    var price =goodsInfo['PRESENT_PRICE'];
    var images= goodsInfo['IMAGE1'];
    return Container(
       width:ScreenUtil().setWidth(750),
       color: Colors.white,
       height: ScreenUtil().setHeight(80),
       child: Row(
         children: <Widget>[
           Stack(
             children: <Widget>[
               InkWell(
                  onTap: (){
                      Provide.value<CurrentIndexProvide>(context).changeIndex(2);
                      Navigator.pop(context);
                  },
                  child: Container(
                      width: ScreenUtil().setWidth(110) ,
                      alignment: Alignment.center,
                      child:Icon(
                            Icons.shopping_cart,
                            size: 35,
                            color: Colors.red,
                          ), 
                    ) ,
                ),
                Provide<CartProvide>(
                  builder: (context,child,val){
                    return  Positioned(
                        top:0,
                        right: 10,
                        child: Container(
                          padding:EdgeInsets.fromLTRB(6, 3, 6, 3),
                          decoration: BoxDecoration(
                            color:Theme.of(context).primaryColor,
                            border:Border.all(width: 2,color: Colors.white),
                            borderRadius: BorderRadius.circular(12.0)
                          ),
                          child: Text(
                            '${val.allGoodsCount}',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: ScreenUtil().setSp(22)
                            ),
                          ),
                        ),
                      ) ;
                  },
                )
             ],
           ),
           InkWell(
             onTap: ()async{
               Provide.value<CartProvide>(context).save(goodsId, goodsName, count, price, images,context);
             },
             child: Container(
               alignment: Alignment.center,
               width: ScreenUtil().setWidth(320),
               height: ScreenUtil().setHeight(80),
               color: Colors.green,
               child: Text(
                 '加入购物车',
                 style: TextStyle(color: Colors.white,fontSize: ScreenUtil().setSp(28)),
               ),
             ) ,
           ),
           InkWell(
             onTap: ()async{
               showDialog<Null>(
                context: context, //BuildContext对象
                barrierDismissible: false,
                builder: (BuildContext context) {
                    return Info(goodsId:goodsId,goodsName:goodsName,price:price,images:images,);
                });
             },
             child: Container(
               alignment: Alignment.center,
               width: ScreenUtil().setWidth(320),
               height: ScreenUtil().setHeight(80),
               color: Colors.red,
               child: Text(
                 '马上购买',
                 style: TextStyle(color: Colors.white,fontSize: ScreenUtil().setSp(28)),
               ),
             ) ,
           ),
         ],
       ),
    );
  }
}

class Info extends Dialog {
  
    String goodsId;
    String goodsName;
    int count =1;
    var price;
    String images;

  Info({Key key, @required this.goodsId,this.goodsName,this.price,this.images}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20)
          ),
          width: ScreenUtil().setWidth(700),
          height: ScreenUtil().setHeight(400),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Image.network(images,width: 100,),
                  Column(
                    children: <Widget>[
                      Text(goodsName),
                      Text('单价${price}元'),
                      Text('购买一份'),
                      Text('总价${price}元',style: TextStyle(color: Theme.of(context).primaryColor),)
                    ],
                  )
                ],
              ),
              Row(
                children: <Widget>[
                  SizedBox(
                    height: 10,
                    width: ScreenUtil().setWidth(300),
                  ),
                  Container(
                    margin: EdgeInsets.only(right: ScreenUtil().setWidth(50)),
                    alignment: Alignment.center,
                    width: ScreenUtil().setHeight(80),
                    height: ScreenUtil().setWidth(80),
                    decoration: BoxDecoration(
                      color: Theme.of(context).primaryColor,
                      borderRadius: BorderRadius.circular(10)
                    ),
                    child: InkWell(
                      child: Text('取消'),
                      onTap: (){
                        Navigator.pop(context);
                      },
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    width: ScreenUtil().setHeight(80),
                    height: ScreenUtil().setWidth(80),
                    decoration: BoxDecoration(
                      color: Theme.of(context).primaryColor,
                      borderRadius: BorderRadius.circular(10)
                    ),
                    child: InkWell(
                      child: Text('确定'),
                      onTap: (){
                        String token = Provide.value<PersonnalProvider>(context).token;
                        String username = Provide.value<PersonnalProvider>(context).username;
                        if(username == '游客'){
                          Fluttertoast.showToast(
                            msg: '请先登录再购买',
                            toastLength: Toast.LENGTH_SHORT,
                            gravity: ToastGravity.BOTTOM,
                            timeInSecForIos: 1,
                            backgroundColor: Theme.of(context).primaryColor,
                            textColor: Colors.white,
                            fontSize: 16.0
                          );
                          return;
                        }
                        Provide.value<DetailsInfoProvide>(context).buy(goodsId, goodsName, count, price, images, context,token,username);
                        
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}